package main

import (
	"fmt"
	"ghw2/namesrv/Mapper"
	"ghw2/namesrv/config"
	"ghw2/namesrv/connect"
	"net"
	"time"
)

func main() {
	if err := config.NamesrvConfigInit("namesrv.yaml"); err != nil {
		return
	}
	Mapper.InitMapper()

	listener, err := net.Listen("tcp", config.NameServerConfig.Namesrv)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 服务端每10秒对服务列表检测一次
	ticker := time.NewTicker(time.Second * 10)

	go func() {
		for range ticker.C {
			Mapper.CheckUpdateAtProcess()
		}
	}()

	fmt.Println("Now nameserver is listening at address", listener.Addr().String(), ":")
	for {
		if conn, err := listener.Accept(); err != nil {
			fmt.Println(err)
			return
		} else {
			fmt.Println("A connection is established from address", conn.RemoteAddr().String())
			go connect.ConnProcess(&conn)
		}
	}
}
